package com.kly.cashmall.widget.refresh;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import androidx.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kly.cashmall.AppApplication;
import com.kly.cashmall.R;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshKernel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;

/**
 * @author chenwei
 * @date 2018/1/22
 */
public class SimpleRefreshHeader extends LinearLayout implements RefreshHeader {

  protected RefreshKernel mRefreshKernel;
  protected int mBackgroundColor;
  protected int mFinishDuration = 400;

  private ImageView refreshIcon;
  private TextView refreshText;

  private String noRefreshStr = AppApplication.getInstance().getString(R.string.srl_header_pulling);
  private String refreshIngStr = AppApplication.getInstance().getString(R.string.srl_header_refreshing);

  private String refreshCompletedStr = AppApplication.getInstance().getString(R.string.srl_header_finish);
  private String canRefreshStr = AppApplication.getInstance().getString(R.string.srl_header_release);

  public SimpleRefreshHeader(Context context) {
    this(context, null);
  }

  public SimpleRefreshHeader(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public SimpleRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    LayoutInflater.from(context).inflate(R.layout.layout_simple_refresh_header, this, true);

    refreshIcon = findViewById(R.id.refresh_icon);
    refreshText = findViewById(R.id.refresh_txt);
  }

  @NonNull
  @Override
  public View getView() {
    return this;
  }

  @Override
  public SpinnerStyle getSpinnerStyle() {
    return SpinnerStyle.Translate;
  }

  @Override
  public void setPrimaryColors(int... colors) {
    if (colors.length > 0) {
      if (!(getBackground() instanceof BitmapDrawable)) {
        setPrimaryColor(colors[0]);
      }
    }
  }

  @Override
  public void setBackgroundColor(int color) {
    super.setBackgroundColor(color);
    mBackgroundColor = color;
  }

  private void setPrimaryColor(int primaryColor) {
    setBackgroundColor(primaryColor);
  }

  @Override
  public void onInitialized(RefreshKernel kernel, int height, int extendHeight) {
    mRefreshKernel = kernel;
    mRefreshKernel.requestDrawBackgroundFor(this, mBackgroundColor);
  }

  @Override
  public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

  }

  @Override
  public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) {

  }

  @Override
  public int onFinish(RefreshLayout layout, boolean success) {

    return mFinishDuration;
  }

  @Override
  public boolean isSupportHorizontalDrag() {
    return false;
  }

  @Override
  public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState,
                             RefreshState newState) {

    switch (newState) {
      case None:
      case PullDownToRefresh:
        refreshIcon.setImageResource(R.mipmap.ic_refresh_loading);
        refreshText.setText(noRefreshStr);
        changeRefreshIconStatus(false);
        break;
      case Refreshing:
      case RefreshReleased:
      case Loading:
        if (refreshIcon.getAnimation() == null) {
          refreshIcon.setImageResource(R.mipmap.ic_refresh_loading);
          refreshText.setText(refreshIngStr);
          changeRefreshIconStatus(true);
        }
        break;
      case ReleaseToRefresh:
        refreshIcon.setImageResource(R.mipmap.ic_refresh_loading);
        refreshText.setText(canRefreshStr);
        changeRefreshIconStatus(false);
        break;
      case RefreshFinish:
      case LoadFinish:
        refreshIcon.setImageResource(R.mipmap.ic_refresh_complete);
        refreshText.setText(refreshCompletedStr);
        changeRefreshIconStatus(false);
        break;
      default:
        refreshIcon.setImageResource(R.mipmap.ic_refresh_loading);
        refreshText.setText(noRefreshStr);
        changeRefreshIconStatus(false);
        break;
    }
  }

  private void changeRefreshIconStatus(boolean loading) {
    if (loading) {
      if (refreshIcon.getAnimation() == null) {
        RotateAnimation rotate = new RotateAnimation(refreshIcon.getRotation() % 360,
            360f + (refreshIcon.getRotation() % 360), Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
        LinearInterpolator lin = new LinearInterpolator();
        rotate.setInterpolator(lin);
        rotate.setDuration(800);
        rotate.setRepeatCount(-1);
        rotate.setFillAfter(true);
        rotate.setStartOffset(0);
        refreshIcon.startAnimation(rotate);
      }
    } else {
      refreshIcon.setRotation(0);
      if (refreshIcon.getAnimation() != null) {
        refreshIcon.clearAnimation();
      }
    }
  }

  @Override
  public void onMoving(boolean isDragging, float percent, int offset, int height,
                       int maxDragHeight) {

    refreshIcon.setRotation(percent * 360);
  }

  @Override
  public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

  }

  public SimpleRefreshHeader setFinishDuration(int mFinishDuration) {
    this.mFinishDuration = mFinishDuration;
    return this;
  }

  @Override
  protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
  }
}
